package com.zyw.zhuangdian.dao.impl;

import com.zyw.zhuangdian.dao.BaseDao;
import com.zyw.zhuangdian.dao.MessageDao;
import com.zyw.zhuangdian.exceptions.MyDaoException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
@Transactional
public class MessageDaoImpl extends BaseDao implements MessageDao {
    @Override
    public void addMessage(Map in) {
        int i = sqlTemplate.insert("MessageDaoImpl.addMessage",in);
        if (i<1){
            throw new MyDaoException("插入消息记录失败");
        }
    }

    @Override
    public void readMessages(Map in) {
        int i = sqlTemplate.insert("MessageDaoImpl.readMessages",in);
        if (i<1){
            throw new MyDaoException("插入已读消息记录失败");
        }
    }

    @Override
    public void unreadMessages(Map in) {
        int i = sqlTemplate.delete("MessageDaoImpl.unreadMessages",in);
        if (i<1){
            throw new MyDaoException("删除已读消息记录失败");
        }
    }

    @Override
    public void updateMessage(Map in) {
        int i = sqlTemplate.update("MessageDaoImpl.updateMessage",in);
        if (i<1){
            throw new MyDaoException("更新消息记录失败");
        }
    }

    @Override
    public void deleteMessage(Map in) {
        int i = sqlTemplate.delete("MessageDaoImpl.deleteMessage",in);
        if (i<1){
            throw new MyDaoException("删除消息记录失败");
        }
    }

    @Override
    public void deleteAllMessage(Map in) {
        int i = sqlTemplate.insert("MessageDaoImpl.deleteAllMessage",in);
        if (i<1){
            throw new MyDaoException("删除消息记录失败");
        }
    }

    @Override
    public List<Map> listMessages(Map in) {
        List<Map> list = sqlTemplate.selectList("MessageDaoImpl.listMessages",in);
        return list;
    }

    @Override
    public int listMessagesCount(Map in) {
        List<Map> list = sqlTemplate.selectList("MessageDaoImpl.listMessagesCount",in);
        if (list.size() < 1){
            return 0;
        }
        return Integer.parseInt(list.get(0).get("count").toString());
    }

    @Override
    public int listUnreadMessageCount(Map in) {
        List<Map> list = sqlTemplate.selectList("MessageDaoImpl.listUnreadMessageCount",in);
        if (list.size() < 1){
            return 0;
        }
        return Integer.parseInt(list.get(0).get("count").toString());
    }

    @Override
    public List<Map> listUnreadMessages(Map in) {
        List<Map> list = sqlTemplate.selectList("MessageDaoImpl.listUnreadMessages",in);
        return list;
    }

}
